package com.yang.entity;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.yang.utils.UserUtils;

/**
 * 描述：用户角色bean
 * 版本：1.0
 * 作者： 杨建全
 * 创建时间：2016年3月16日  下午5:34:40
 */
@Entity
@Table(name="t_role")
public class Roles {
	
	private String id;			//主键
	private String rolename;	//角色名称
	private Set<User> users;	//角色用户
	private Set<Permissions> permissions;	//角色拥有的权限
	private String premissionsStr;	//权限id字符串，用于前端传送	非数据库存储
	
	@Id
	@GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
	@Column(name = "rid", nullable = false, length = 9)
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "rolename", nullable = false, length = 20)
	public String getRolename() {
		return rolename;
	}
	public void setRolename(String rolename) {
		this.rolename = rolename;
	}
	
	@Transient
	public String getPremissionsStr() {
		if(null!=permissions){
			premissionsStr="";
			if(UserUtils.isRoleHasAllPermisson(this)){
				return "ALL";
			}else{
				for (Permissions pers : permissions) {
					premissionsStr = premissionsStr+pers.getId()+",";
				}
				return premissionsStr.substring(0, premissionsStr.length()-1);
			}
		}
		return premissionsStr;
	}

	public void setPremissionsStr(String premissionsStr) {
		this.premissionsStr = premissionsStr;
	}
	
	
	@ManyToMany(fetch=FetchType.LAZY,mappedBy="roles")
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@OneToMany(cascade = { CascadeType.PERSIST},mappedBy="role",fetch=FetchType.LAZY)
	public Set<Permissions> getPermissions() {
		return permissions;
	}
	public void setPermissions(Set<Permissions> permissions) {
		this.permissions = permissions;
	}
	
}
